package com.android.qjpmc.data_module;

import com.android.qjpmc.bean.PwrDeviceBean;
import com.android.qjpmc.bean.PointParameterBean;

import java.util.ArrayList;
import java.util.List;

public class PwrDevice {

    public String getM_Name() {
        return m_Name;
    }

    PwrDeviceBean m_device_bean;
    private String m_Name;
    private int m_ID;//
    public PwrDevice m_NextNode = null;
    private PwrDevice m_PreNode = null;
    private DataTeam m_AnalogDataTeam = null;//模拟量数据
    private DataTeam m_DigtalDataTeam = null;//数字量数据
    private DataTeamSet DataTeamSet = new DataTeamSet();
    private int m_PointDataLoaded = 0;

    //设置测点被加载的标志
    public void SetPointLoadedMark(int Mark)
    {
        m_PointDataLoaded = Mark;
    }
    //是否测点被加载
    public int IsPointLoaded()
    {
        return m_PointDataLoaded;
    }
    public  PwrDevice()
    {
        m_AnalogDataTeam = new DataTeam();
        m_DigtalDataTeam = new DataTeam();
        DataTeamSet.AppendANode(m_AnalogDataTeam);
        DataTeamSet.AppendANode(m_DigtalDataTeam);
    }
    public int AppendANode(PwrDevice Node)
    {
        PwrDevice NextNode = GetNext();
        if (Node == null)
        {
            return 1;
        }
        SetNext(Node);
        Node.SetNext(NextNode);
        if (NextNode != null)
        {
            NextNode.SetPre(Node);
        }
        return 0;
    }
    public void SetID(int ID)
    {
        m_ID = ID;
    }
    public void SetName(String Name)
    {
        m_Name = Name;
    }
    public int GetID()
    {
        return  m_ID;
    }
    public int IsMe(int ID)
    {
        if (m_ID == ID)
        {
            return 1;
        }
        return 0;
    }
    public PwrDevice GetNext()
    {
        return m_NextNode;
    }
    public PwrDevice GetPre()
    {
        return m_PreNode;
    }
    public void SetNext(PwrDevice Device)
    {
        m_NextNode = Device;
    }
    public void SetPre(PwrDevice Device)
    {
        m_PreNode = Device;
    }
    DataTeam GetFristDataTeam()
    {
        if (DataTeamSet == null)
        {
            return null;
        }
        return DataTeamSet.GetHead();
    }
    DataTeam GetAnalogDataTeam()
    {
        return m_AnalogDataTeam;
    }
    DataTeam GetDigitalDataTeam()
    {
        return m_DigtalDataTeam;
    }

    DataTeam GetDataTeam(byte Type)
    {
        switch(Type)
        {
            case DataTeam.DATA_TYPE_ANALOG:
            {
                return m_AnalogDataTeam;
            }
            case DataTeam.DATA_TYPE_SIGNAL:
            {
                return m_DigtalDataTeam;
            }
        }
        return null;
    }


    DataTeam CreateDataTeamData(byte DataTeamType, int Count)
    {
        DataTeam theDataTeam = null;
        switch(DataTeamType)
        {
            case DataTeam.DATA_TYPE_ANALOG:
            {
                theDataTeam = GetAnalogDataTeam();
            }
            break;
            case DataTeam.DATA_TYPE_SIGNAL:
            {
                theDataTeam = GetDigitalDataTeam();
            }
            break;
        }
        if (theDataTeam != null)
        {
            theDataTeam.CreateTeamData(DataTeamType, Count);
        }
        return theDataTeam;
    }

    public ArrayList<AnalogDataItem> GetAnalogDataList()
    {
        if (m_AnalogDataTeam == null)
        {
            return  null;
        }
        return m_AnalogDataTeam.GetAnalogValueList();
        /*
        ArrayList<AnalogDataItem> = new
        DataTeam AnalogDataTeam = GetAnalogDataTeam();
        if (AnalogDataTeam == null)
        {
            return null;
        }
        AnalogDataTeam
         */
    }

    public AnalogDataItem GetAnalogDataItem(int index)
    {
        ArrayList<AnalogDataItem> list =  GetAnalogDataList();
        if (list == null)
        {
            return null;
        }
        if (index < 0)
        {
            return null;
        }
/*
不是按照索引判断,按照组内ID判断,所以不需要比较越界
        if (index >= list.size())
        {
            return null;
        }
*/
        AnalogDataItem item = null;
        for (int idx = 0; idx < list.size(); idx++)
        {
            item = list.get(idx);
            if (item != null)
            {
                if (item.m_IDInGroup == index)
                {
                    return item;
                }
            }
        }
        return null;
    }

    public AnalogDataItem GetAnalogDataItemByIndex(int index)
    {
        ArrayList<AnalogDataItem> list =  GetAnalogDataList();
        if (list == null)
        {
            return null;
        }
        if (index < 0)
        {
            return null;
        }

        if (index >= list.size())
        {
            return null;
        }
        AnalogDataItem item = list.get(index);

        return item;
    }

    public void SetDeviceBean(PwrDeviceBean bean)
    {
        m_device_bean = bean;
        if (m_device_bean != null)
        {
            m_ID = Integer.parseInt(m_device_bean.groupId);
            m_Name = m_device_bean.deviceName;
        }
    }

    public PwrDeviceBean GetDeviceBean()
    {
        return m_device_bean;
    }

    public String GetDeviceID()
    {
        return Integer.toString(GetID());
    }

    public int CreateAnalogTeam(List<PointParameterBean> pointList)
    {
        DataTeam team = GetAnalogDataTeam();
        if (team == null)
        {
            return 1;
        }
        return team.CreateAnalogTeam(pointList);
    }
    public int UpdateRealtimeValues()
    {
        PwrDeviceBean bean = GetDeviceBean();
        DataTeam team = GetAnalogDataTeam();
        if ( (team == null) || (bean == null) )
        {
            return 1;
        }
        return team.UpdateAnalogTeam(bean.pointList);
    }

    public List<PointParameterBean> GetAnalogParametesBeanList()
    {
        DataTeam team = GetAnalogDataTeam();
        if (team == null)
        {
            return null;
        }
        return team.GetParameterBeanList();
    }

}
